<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/10
  Time: 16:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>首页--汽车租赁系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
</head>

<body class="childrenBody">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>

<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="rolename" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入角色名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">角色描述:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="roledesc" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入角色描述" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
        <label class="layui-form-label">是否可用:</label>
        <div class="layui-input-inline">
            <input type="radio" name="available" id="available_on" value="1" checked="checked" title="可用">
            <input type="radio" name="available" id="available_off" value="0" title="不可用">
        </div>
    </div>
        <div class="layui-inline">
            <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit  lay-filter="doSearch">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>
</form>

<table id="roleTable" lay-filter="roleTable" ></table>

<script id="toolbarRole" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="roleAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="delBatch">批量删除</button>
    </div>
</script>

<script id="barRole" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="selRoleMenu">分配菜单</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
</script>

<%-- 分配角色开始 --%>
<div style="display: none;padding: 20px" id="selRoleMenuDiv">
    <ul id="menuTree" class="dtree" data-id="0" style="width: 100%;"></ul>
</div>


<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">

        <div class="layui-form-item">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="roleid" id="roleid">
                <input type="text" name="rolename" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色描述:</label>
            <div class="layui-input-block">
                <input type="text" name="roledesc" placeholder="请输入角色描述" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否可用:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" checked title="可用">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit>提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/index.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/cache.js"></script>
<script>
    var tab;
    layui.extend({
        dtree: '${pageContext.request.contextPath}/static/layui_ext/dtree/dtree'
    }).use(['table','form','dtree','jquery'],function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        var dtree = layui.dtree



        tab = table.render({
            elem: '#roleTable',
            url: "${pageContext.request.contextPath}/role/roleShow",
            cellMinWidth: 30,
            page: {limit: 5, limits: [5, 10, 15, 20]},
            // id:'deleteAll',
            toolbar: '#toolbarRole',//开启头部工具栏，并为其绑定左侧模板
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'roleid', width: 145, title: '角色ID', sort: true}
                , {field: 'rolename', width: 200, title: '角色名称', sort: true}
                , {field: 'roledesc', width: 300, title: '角色权限'}
                , {fixed: 'right', title: '操作', toolbar: '#barRole', width: 160}
            ]]
        })
        var index;

        //头工具栏事件
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event == 'roleAdd') {
                roleAdd()
            } else if (obj.event == 'delBatch') {
                delAll()
            }
        })
        function delAll(){
            var data1 = table.checkStatus('roleTable').data
            // var data1=checkStatus.data;
            var ids="";
            var count=0;
            for(var i=0;i<data1.length;i++){
                ids+=data1[i].roleid+",";
                count++;
            }

            if(count == 0){
                return;
            }
            ids = ids.substring(0,ids.length-1)

            layer.confirm("确定删除这"+count+"条数据吗",function(r){
                layui.$.get("${pageContext.request.contextPath}/role/delAll?ids="+ids,function(rr){
                    layer.msg(rr.msg)
                    tab.reload()
                })
            })
        }


        function roleAdd() {
            //打开弹出框
            index = layer.open({
                type: 1,
                title: '添加角色',
                content: $("#saveOrUpdateDiv"),
                area: ['800px', '440px'],
                success: function () {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset();
                }
            })
        }
        form.on('submit(doSearch)', function (data) {
            //ajax数据
            tab.reload({
                where: data.field
            })
            //不跳转页面
            return false;
        })
        form.on('submit(doSubmit)',function(data){
            var param = $("#dataFrm").serialize()
            layer.confirm("确定要提交该数据么", function (r) {
                $.get("${pageContext.request.contextPath}/role/addRole?"+param,function (r) {
                    layer.msg(r.msg)
                    layer.close(index)
                    tab.reload()
                })
            })

            return false;

        })
        table.on('tool(roleTable)', function(obj){
            var data = obj.data
            if(obj.event == "edit"){ // 编辑
                roleUpdate(data)
            }else  if(obj.event == "del"){ // 删除
                roleDelete(data.roleid)
            }else if(obj.event == "selRoleMenu"){ // 分配角色
                selRoleMenu(obj.data)
            }
        })
        function roleDelete(roleid){
            layer.confirm("确定要删除该数据吗？",function(){
                $.post("${pageContext.request.contextPath}/role/roleDelete",{"roleid":roleid},function(r){
                    layer.msg(r.msg)
                    tab.reload()
                })
            })
        }

        function roleUpdate(data){
            index = layer.open({
                type: 1,
                title: '修改角色',
                content: $("#saveOrUpdateDiv"),
                area: ['800px', '440px'],
                success: function () {
                    // 给form表单设置值
                    // 把当前行的数据data显示到form表单中去,form表单中是有id隐藏字段
                    form.val("dataFrm", data)
                    // 下拉框设置值
                    if (data.roleid != 0) {
                     //把roleid保存到隐藏字段
                        $("#roleid").val(data.roleid)
                    }
                    // 给pid隐藏字段赋值


                }
            })
        }
        <%-- 分配角色结束 --%>
//监听行工具事件



        function selRoleMenu(data) {
            index = layer.open({
                type : 1
                , title:"分配"+data.rolename+"角色"
                , content:$("#selRoleMenuDiv")
                ,area :['400px','500px']
                , btn:['<div class="layui-icon layui-icon-release">确认分配</div>'
                    ,'<div class="layui-icon layui-icon-close">取消分配</div>']
                ,btnAlign:'c'
                ,success:function () {
                    // 给下拉数赋值
                    dtree.render({
                        elem: "#menuTree",
                        url: "${pageContext.request.contextPath}/role/roleTreeJson?roleid="+data.roleid // 使用url加载（可与data加载同时存在）
                        , dataStyle:"layuiStyle"//使用layui风格的数据格式
                        , response:{message:"msg",statusCode:0}  //修改response中返回数据的定义
                        , checkbar : true
                        , checkbarType:"all"
                        , chackbarData:"choose"
                    });
                }
                , yes : function(){
                    // 获取菜单数中选中的nodes
                    var nodes = dtree.getCheckbarNodesParam("menuTree");
                    // 获取角色id
                    var roleid = data.roleid
                    var param = "roleid="+roleid

                    // 循环选中的内容nodes
                    $.each(nodes,function(i,item){
                        param += "&ids=" +item.nodeId
                    })
                    // A=valueA&B=valueB&B=valueB
                    // 把数据保存到数据库
                    $.get("${pageContext.request.contextPath}/role/saveRoleMenu?"+param,function (r) {
                        layer.msg(r.msg)
                    })
                    layer.close(index)
                }
            })
        }


    })

</script>

</body>
</html>
